#ifndef _IFAPP_H
#define _IFAPP_H
#ifdef __cplusplus
extern "C" {
#endif
#define TYPE_GSM   0x0002
#define TYPE_LTE   0x0004
#define TYPE_CDMA  0x0005
#define TYPE_GSM_LTE 0x000b

#if 0
#define IF_APP_EVENT_AUTH_SERVER	0x100
#endif
#define IF_APP_EVENT_MNG_SERVER		0x101
#define IF_APP_EVENT_DATA_SERVER	0x102
#define IF_APP_EVENT_TIMER			0x103
#define IF_APP_EVENT_TIMEOUT		0x104
#define IF_APP_EVENT_DEVICE			0x105
#define IF_APP_EVENT_INIT_DEV		0x106
#define IF_APP_EVENT_INIT_CLI		0x107
#define IF_APP_EVENT_CLI_STOP		0x108
#define IF_APP_EVENT_SYNC_CONF		0x109
#define IF_APP_EVENT_GET_BBU_INFO       0x10a
#define IF_APP_EVENT_LOCAL_CONFIG   0x10b

#define IF_APP_EVENT_HANDLE_EUTRAN  0x10c
#define IF_APP_EVENT_IMSI_DATA      0x10d    
#define IF_APP_EVENT_IMSI_MNG      0x10f    
#define IF_APP_EVENT_UDP_SERVER    0x110
//#define IF_APP_EVENT_RESTART_CLI        0x10b
#define TIME_DATA_REDUNDENCY		120	//120s to try to cover it.

#define CLI_APP_TICKS			50  //50ms, 20Hz
#define CLI_APP_1S				1000    //1000ms

#define TIMEOUT_CMD_QUEUE		0x0fffffff
#define TIMEOUT_CMD_PENDING		250
#define TIMEOUT_DATA_PENDING	CLI_APP_1S
#define TIMEOUT_MNG_INTVL(x)	(CLI_APP_1S * x)
#define TIMEOUT_SRV_DEAD(x)		(3 * CLI_APP_1S * x)
#define TIMEOUT_DATA_INTVL(x)	TIMEOUT_MNG_INTVL(x)
#define TIMEOUT_UPDATA_FB(x)	(TIMEOUT_DATA_INTVL(x) - CLI_APP_1S)
#define TIMEOUT_STATISTICS_INTVL(x)    TIMEOUT_MNG_INTVL(x)
#define TIMEOUT_STATISTICS_FB(x)      (x * CLI_APP_1S)
#define TIMEOUT_RESTART_CLI		(CLI_APP_1S * 10)
#define TIMEOUT_GET_STATUS(x)	(x * CLI_APP_1S)
#define TIMEOUT_DEV_DEAD(x)		( 10 * x * CLI_APP_1S)
#define TIMER_OUT_RESTART_CREATE_PTHREAD (CLI_APP_1S * 5)
#define TIMER_OUT_RESTART_AUTH   (CLI_APP_1S * 15)
#define TIMEOUT_SNIFFER_DOING    (CLI_APP_1S * 2)
#define TIMEOUT_CMD_NO_RSP   (CLI_APP_1S * 20)
#define TIMEOUT_CHNG_TAC_CYCLE(x)  (CLI_APP_1S * x)
#define TIMEOUT_CFG_FB            (CLI_APP_1S * 6)

#define TIMEOUT_REQUEST_BBU_STATE    (CLI_APP_1S * 120)
#define TIMEOUT_REBOOT_BBU           (CLI_APP_1S * 5)

#define TIMEOUT_REPORT_BBU_STATE   (CLI_APP_1S * 120)

#define TIMEOUT_UPLOAD_OLD_DATA    (CLI_APP_1S)

#define TIMEOUT_BBU_CONFIG_CELL     (CLI_APP_1S *5) //check every bbu state 
#define TIMEOUT_REPORT_LOCAL_CONFIG  (CLI_APP_1S *10) //to do send local config to service
#define TIMEOUT_MANUAL_SNF            (CLI_APP_1S *90) //wait for do manual snf again
#define TIMEOUT_RESTART_UPGRADE_BBU   (CLI_APP_1S * 120)
#define TIMEOUT_INTERNAL_HEARTBEAT     (CLI_APP_1S * 5)
#define TIMEOUT_CHECK_NETWORK_STATE   (CLI_APP_1S * 2)
#define TIMEOUT_RESTART_SEND_DATA     (CLI_APP_1S * 60) 

#define TIMEOUT_UPGRADE_FIALED         (CLI_APP_1S * 2400)
#define TIMEOUT_LTE_POWER_ON           (CLI_APP_1S * 2)

#define TIMEOUT_SNIFFER_FAILED          (CLI_APP_1S * 120)

#define LOCAL_CONFIG_SOTR          1
#define LOCAL_CONFIG_DATA_INTVL    2
#define LOCAL_CONFIG_DET_INTVL     3
#define LOCAL_CONFIG_STATUS_INTVL  4
#define LOCAL_CONFIG_DATA_CYCLE    5
#define LOCAL_CONFIG_INTERFACE     6
#define LOCAL_CONFIG_WIRELESS      7
#define LOCAL_CONFIG_SNIFFER       8
#define LOCAL_CONFIG_SMS_KEY       9
#define LOCAL_CONFIG_END           10

#define DATA_TYPE_IMSI   0
#define DATA_TYPE_MAC    1
typedef struct {
	double longitude;
	double latitude;
	double altitude;
	
} GPSINFO;

typedef struct{
	double longitude;
	double latitude;
	int starcnt;
}GPSBOARDINFO;

typedef struct _cmd_q {
    struct _cmd_q *next;
    I32 fcode;
    I32 socket;
    I32 timeout;                //timeout in tickets.
    I32 dcode;
    I32 id;
    I32 state;
    I32 type;                 //cmd type
} CMD_QUEUE;
#define CMD_QUEUE_SIZE		512
#define EVENT_QUEUE_SIZE	1024
//#define MAX_DATA_PER_FRAME	100
#define MD5_OFFSET			1024
#define  MAX_RECORD_NUM     200
typedef struct {
    I32 module;                 //module
    I32 socket;                 //for data event socket id, to pass to cmd queue. for timer event, it is unique ID
    I32 len;                    //data len
    I8 *data;                   //data pointer
} EVENT_QUEUE;

enum ListState
{
    IDEL = 0,
    RECVING = 1,
    UPLOADING = 2
};


#define  BBU_NOT_EXIST  -1
#define  BBU_INITALIZING 0
#define  BBU_REDAY       2
#define  BBU_SNFING      3
#define  BBU_CELL_CFG    4
#define  BBU_LOCATION    5
#define  BBU_INIT_CFG    6
#define  BBU_INACTIVE	 7 
#define  BBU_UPGRADE     8
#define  BBU_UPGRADING   9
#define  BBU_UPGRADE_SUCCESS  10
#define  BBU_UPGRADE_FAILED -2

#define  NOT_START           0
#define  INIT_SYNCED_FAILED  1
#define  INIT_SYNCED         2
#define  CNM_FAILED          3
#define  CNM_SYNCED          4
#define  OUT_OF_SYNC         5
#define  GPS_NOT_SYNC        6
#define  GPS_SYNC            7
#define  INIT_UART_FAILED    8
#define  GPS_LOCK_FAILED     9
#define  NO_GPS_AND_NEIGHBOURE 10
#define  GPS_INITALIZING     11





typedef struct{
    U8 ID;
    U8 state;
    S8 temp;
    U8 upatt;
    U8 dnatt;
    S8 dn_power;
    I16 dn_10_power; //downlink power * 10
    S8 sw10ratio; 	//vswr * 10
    S8 tempthres;
	S8 powerthres;
} PA_STATUS;

typedef struct {
    I32 bbuState;
    I32 syncFialTm;
    I32 memusage;
    I32 cpuusage;
    I32 cputemp;
    I32 boardtemp;
    I32 syncType;
    I32 syncState;
} BBU_STATUS;

typedef struct {
    I32 isActive;
    U16 lac;
    U16 freq;
} REAL_TIME;

typedef struct {
    U32 cpu_load;
    U32 mem_load;
    U32 disk_usage;
    I32 dev_temp;
    I32 board_tmp;
    I32 gsm_status;
    I32 sms_status;
    I32 snf_status;
    U16 tddLocationEnable;
    U16 fddLocationEnable;
    BBU_STATUS bbuStatus[7];
    PA_STATUS pa_status[11];
    REAL_TIME  realTimeStatus[13];
} DEV_STATUS;



typedef struct {
    U32 time_low;
    U32 time_high;
    U16 slotId;
    U16 lac;
    U8 imsi[16];
    U8 imei[16];
    U8 tmsi[16];
} PHONE_DATA;

typedef struct {
    U32 time_low;
    U32 time_high;
    U8 mac[14];
} MAC_DATA;

typedef struct tagPHONEINFO {
    U32 uptime;
    U16 slotId;
    U16 lac;
    I8 imsi[16];    
    I8 imei[16];
} PHONEINFO;

typedef struct {
    U32 num;
    I8 data[4];
} PHONE_TMP;

typedef struct tagRECORDINFO
{
    I32 maxId;
    I32 minId;
    I32 lastId;
    I32 realNum;
    I32 oldId;
    I32 startId;
    I32 endId;
    PHONEINFO phoneInfoBuf[MAX_RECORD_NUM]; 
} RECORDINFO;


typedef struct tagMACINFO {
    U32 uptime;
    I8 mac[14];
} MACINFO;

typedef struct tacMACRECORDINFO
{
    I32 maxId;
    I32 minId;
    I32 lastId;
    I32 realNum;
    I32 oldId;
    MACINFO macInfoBuf[MAX_RECORD_NUM];
} MACRECORDINFO;

typedef struct tagRECVLIST
{
    I32 state;
    I32 num;
    PHONEINFO phoneInfoBuf[MAX_RECORD_NUM];     
} RECVLIST;

typedef struct {
    U32 num;
    U32 len;
    U32 time;
    I8 data[4];
} UPDATA_CACHE;

extern I32 slotNum[9];
typedef struct {
   U8 mac_addr[32];
   I8 dev_type[16];
   I8 sw_version[16];
   I8 build_date[16];
   I8 build_time[16];
   I8 compliter_verison[128];
   I8 os_version[16];
   I8 hw_version[16];
   I8 sw_originalVer[16];
   I8 currVernum[4];
} SYS_INFO;

typedef struct {
   I8 software_version[32];
   I8 phy_ver[32];
   I8 kernel_ver[32];
   I32 slotId;
} SOFTWARE_VERSION_INFO;


typedef struct {
	U16 freq;
	U16 pci;
	U16 tac;
	U16 rssi;
    U16 priority;
} SNF_RSLT;

typedef struct {
	I32 slotId;
	I32 freq;
	I32 pci;
	I32 plmnId;
	I32 tac;
	U16 type;
    U16 listNum;
	U16 pciList[6];
	U16 freqList[6];
} CHANG_CELL;

typedef struct{
    I32 rssi;
    I32 Auto;
    U16 freqList[12]; // [lowfreq, upfreq]
} SNIFFER_PAR;

typedef struct {
    I32 rruNumber[6];
    I32 paNumber[6];
} RRU_PA_INFO;

typedef struct {
    I8 server[10][128];
    I32 num;
} NTP_SERVER;

typedef struct GPSPosition {
    double longitude;
    double latitude;
    double altitude;
} GPSPOSITION ;

typedef struct {
    I32 enable;
    I32 slotId;
    I32 ranType;
    I32 band;
    U16 freqList[6];
    U8  imsiList[6][16];
} UE_REDIRECT;

typedef struct {
    I8 port[8];
    I8 user[128];
    I8 password[128];
    I8 serverAddr[128];
} REMOTE_SSH;

typedef struct {
    I32 cmdId;
    I32 cellId;
    I32 result;
    I8 data[128];
} COMMAND;

typedef struct {
    COMMAND commandList[CELL_NUM];
    I32 commandNum;
    I32 cmdIndex;
} COMMANDLIST;

typedef struct {
    U32 uptime;
    U32 seqNum;
    U16 lac;
    I8 imsi[16];    
    I8 imei[16];
} ZHONGDUNDATA;

typedef struct {
    U8 num;
    ZHONGDUNDATA  zhongdunList[100];
} ZHONGDUNLIST;

typedef struct{
    I32 seqNum;
    I8 date[12];
} SEARCHRECORD;

typedef struct {
    U8 num;
    SEARCHRECORD record[50];
} ZHONGDUN_DB_LIST;

typedef struct {
    U16 sn;
    U16 len;
    I8 data[240];
    I8 crc[42];
} ZHONGDUN_PACKET;

typedef struct {
    I8 imsi[16];
    U32 delay;
    U32 sinr;
    I32 freq;
    I32 slotId;
    I32 rsrp;
    I32 pci;
} DELAY_REPORT;


extern COMMANDLIST cmdList;
extern  DEV_STATUS dev_status;
extern int MAX_DATA_PER_FRAME ;
extern GPSBOARDINFO gpsboardinfo;
I32 event_queue_add(I32 module, I32 socket, I32 len, I8 * data);
__inline const I8 *shortname(const I8 *filename);
__inline void cli_app_savelog(I32 level, const I8 *filename, U32 line,  I8 *fmt, ...);
__inline void lockThread();
__inline void unlockThread();
void getmac(char *eth1Mac, char *mac12);
#define CLI_LOG_EMERG           0
#define CLI_LOG_ALERT           1
#define CLI_LOG_CRITICAL		2
#define CLI_LOG_ERROR			3
#define CLI_LOG_WARNING         4
#define CLI_LOG_NOTICE			5
#define CLI_LOG_INFO			6
#define CLI_LOG_DEFAULT			7
#define cli_app_log(level, fmt, args...) lockThread();\
cli_app_savelog(level, shortname(__FILE__), __LINE__,  (char *)fmt, ##args);\
unlockThread();
#ifdef __cplusplus
}
#endif
#endif
